﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using dbPoll_Application.Models;
using System.Text;

namespace dbPoll_Application.Helpers
{
    public class Email
    {
        public static void SendTemplatedEmail(string from, string to, string subject, string templatePath)
        {
            MailMessage mm = new MailMessage(from, to);
            mm.Subject = subject;
            mm.Body = GetEmailBody(templatePath);

            if (mm.Body != "")
            {
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.Credentials = new System.Net.NetworkCredential("donotreplydbpoll@gmail.com", "awesome11");
                smtp.Send(mm);
            }
        }

        public static void SendPlainEmail(string from, string to, string subject, string body)
        {
            MailMessage mm = new MailMessage(from, to);
            mm.Subject = subject;
            mm.Body = body;

            if (mm.Body != "")
            {
                mm.IsBodyHtml = false;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.Credentials = new System.Net.NetworkCredential("donotreplydbpoll@gmail.com", "awesome11");
                smtp.Send(mm);
            }
        }

        public static String CreateAssignBody(String userName, String pollName, String roleName)
        {
            StringBuilder message = new StringBuilder();
            message.AppendLine("Hello " + userName + ",");
            message.AppendLine();
            message.Append("This is a message from dbPoll to inform you that you have been assigned the role of " + roleName + " for the poll: " + pollName + ". Please visit the website for more information.");
            message.AppendLine();
            message.AppendLine();
            message.Append("Regards, the team at dbPoll.");

            return message.ToString();
        }

        public static void SendTemplatedGenericEmails(string from, List<GenericEmail> emails, string subject, string templatePath)
        {
            string emailBody = GetEmailBody(templatePath);
            foreach (GenericEmail email in emails)
            {
                try
                {
                    MailMessage mm = new MailMessage(from, email.Email);
                    mm.From = new MailAddress(from);
                    mm.Subject = subject;
                    mm.Body = emailBody;
                    if (mm.Body != "")
                    {
                        mm.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.gmail.com";
                        smtp.Port = 587;
                        smtp.EnableSsl = true;
                        smtp.Credentials = new System.Net.NetworkCredential("donotreplydbpoll@gmail.com", "awesome11");
                        smtp.Send(mm);
                    }
                }
                catch (Exception e)
                { }
            }
        }

        public static string GetEmailBody(string templatePath)
        {
            System.Net.WebClient objWebClient = new System.Net.WebClient();
            byte[] aRequestedHTML = null;
            string strRequestedHTML = null;

            aRequestedHTML = objWebClient.DownloadData(templatePath);
            System.Text.UTF8Encoding objUTF8 = new System.Text.UTF8Encoding();
            strRequestedHTML = objUTF8.GetString(aRequestedHTML);

            return strRequestedHTML;
        }
    }
}
